package pkmn;

import pkmn.status.StatusEnum;
import pkmn.util.Util;

public class Trainer {
    private String id;
    private String name;
    private Pokemon[] pokemons;
    private Bag bag;
    private int money;
    
    public Trainer(String name){
        this.id = Util.generateId();
        this.name = name;
        this.pokemons = new Pokemon[6];
        this.bag = new Bag();
        this.money = 0;
    }
    
    public String getID(){
        return this.id;
    }
    
    public String getName(){
        return this.name;
    }
    
    public Pokemon getPokemon(int index){
        return this.pokemons[index];
    }

    public Pokemon[] getPokemon(){
        return this.pokemons;
    }

    public boolean addPokemon(Pokemon pokemon){
        for (int index = 0; index < this.pokemons.length; index++){
            if (this.pokemons[index] == null){
                this.pokemons[index] = pokemon;
                return true;
            }
        }
        return false;
    }

    public void removePokemon(int index){
        this.pokemons[index] = null;
    }

    public Pokemon replacePokemon(int index, Pokemon pokemon){
        Pokemon p = this.pokemons[index];
        this.pokemons[index] = pokemon;
        return p;
    }

    public Bag getBag(){
        return this.bag;
    }

    /**
     * Get money.
     * @return int
     */
    public int getMoney() {
        return this.money;
    }

    /**
     * Set the money.
     * @param money
     */
    public void setMoney(int money) {
        this.money = money;
    }

    /**
     * Add a amount of money.
     * @param money int
     */
    public void addMoney(int money) {
        this.money += money;
    }

    /**
     * Check if the trainer have avaliable pokémons
     * @return
     */
    public boolean haveAvaliablePokemon(){
        for (int i = 0; i < 6 && this.pokemons[i] != null; i++){
            if (!this.pokemons[i].getStatus().equals(StatusEnum.fainted)){
                return true;
            }
        }
        return false;
    }
    
    private String getPokemonNames(){
        String ret = "";
        for (int i = 0; i < 6 && this.pokemons[i] != null; i++){
            ret += "\n-" + this.pokemons[i].getName();
        }
        if (ret.equals(""))
            ret = "\nNone";
        return ret;
    }

    public String getPokemonList(){
        String ret = "";
        for (int i = 0; i < 6; i++){
            if (this.pokemons[i] == null)
                ret += "- ";
            else if(this.pokemons[i].getStatus().equals(StatusEnum.none))
                ret += "O ";
            else if(this.pokemons[i].getStatus().equals(StatusEnum.fainted))
                ret += "X ";
            else if(!this.pokemons[i].getStatus().equals(StatusEnum.none))
                ret += "® ";
        }
        return ret;
    }

    @Override
    public String toString(){
        return "ID: " + this.id +
               "\nName: " + this.name +
               "\nPokémons on hand: " + this.getPokemonNames() +
               "\nMoney: $" + this.money;
    }
}
